//
//  HomeGoodsDetailBottomView.m
//  FD
//
//  Created by Yasin on 2018/10/19.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//
//商品详情底部view
#import "HomeGoodsDetailBottomView.h"
@interface HomeGoodsDetailBottomView()
/**购买个数 默认是 1*/
{
    NSInteger _cnt;
}

/**1.折扣价*/
@property (nonatomic,  strong) UILabel *discountPriceLb;
/**2、原价 12-5隐藏 看我❌❌
 2019-1-10显示
 */
@property (nonatomic,  strong) UILabel *originPriceLb;
/**3.加购物车按钮*/
@property (nonatomic,  strong) UIButton *addCartButtton;

/**加减按钮*/
@property (nonatomic,  strong) UIView *minusAddView;
/**减号按钮*/
@property (nonatomic,  strong) UIButton *minusBtn;
/**个数 文字 默认是1*/
@property (nonatomic,  strong) UILabel *countLb;
/**加号按钮*/
@property (nonatomic,  strong) UIButton *addBtn;




@end


@implementation HomeGoodsDetailBottomView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //设置默认值
        _cnt = 1;
        [self setupView];
    }
    return self;
}

- (void)setupView {
    self.backgroundColor = rgba(245, 245, 245, 1.0); //背景灰色
    
    [self addSubview:self.discountPriceLb];
    [self addSubview:self.originPriceLb];
    [self addSubview:self.minusAddView];
    [self addSubview:self.addCartButtton];
    
    [self.minusAddView addSubview:self.minusBtn];
    [self.minusAddView addSubview:self.countLb];
    [self.minusAddView addSubview:self.addBtn];
}

//!!!!:2019-1-20 Rewrite
- (void)layoutSubviews {
    [super layoutSubviews];
    [self.discountPriceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
//        make.size.mas_equalTo(CGSizeMake(50, 22));
        make.height.mas_equalTo(22);
        make.width.mas_greaterThanOrEqualTo(50); //防止数值过大时候显示不全
        make.centerY.equalTo(self.mas_centerY);
    }];
    [self.originPriceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.discountPriceLb.mas_right).offset(2);
        make.height.mas_equalTo(22);
        make.width.mas_greaterThanOrEqualTo(50); //防止数值过大时候显示不全
//        make.size.mas_equalTo(CGSizeMake(50, 22));
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    
    
    [self.minusAddView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.right.equalTo(self.addCartButtton.mas_left);
        make.bottom.equalTo(self.mas_bottom);
        make.width.mas_equalTo(80); //20 +30 +20
    }];
    [self aloneSettingLayoutMinuAddView]; //加减按钮view(w:100)
    
    
    
    [self.addCartButtton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.equalTo(self);
        make.width.mas_equalTo(120);
    }];
    [self.addCartButtton leftTextRightImageWithMargin:5]; //12-31左文字右图
}

- (void)aloneSettingLayoutMinuAddView {

    //-
    [self.minusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.centerY.equalTo(self.minusAddView.mas_centerY);
        make.left.equalTo(self.minusAddView.mas_left);
    }];
    
    //数量:5
    [self.countLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.minusBtn.mas_right);
        make.right.equalTo(self.addBtn.mas_left);
        make.height.mas_equalTo(30);
        make.centerY.equalTo(self.minusAddView.mas_centerY);
    }];
    
    //➕
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20,20));
        make.right.equalTo(self.minusAddView.mas_right).offset(-5);
        make.centerY.equalTo(self.minusAddView.mas_centerY);
    }];
}


//- (void)setupView
//{
//    self.backgroundColor = rgba(245, 245, 245, 1.0); //背景灰色
////    CGFloat y = // 30 40
//    CGFloat h = 30;
//    CGFloat y = (40 - h)*0.5;
//    CGFloat selfW = self.bounds.size.width;
//    CGFloat selfH = self.bounds.size.height;
//    self.discountPriceLb.frame = CGRectMake(10, y, 60, h);
//    self.originPriceLb.frame = CGRectMake(self.discountPriceLb.rb_right, y, 60, h);
//    self.minusAddView.frame =  CGRectMake(self.originPriceLb.rb_right , y, 80, h);
//
//    self.addCartButtton.frame = CGRectMake(self.minusAddView.rb_right, 0, selfW-self.minusAddView.rb_right, selfH);
//    [self.addCartButtton leftTextRightImageWithMargin:5]; //12-31左文字右图
//
//    [self addSubview:self.discountPriceLb];
//    [self addSubview:self.originPriceLb];
//    [self addSubview:self.minusAddView];
//    [self addSubview:self.addCartButtton];
//
//    //中间的加减按钮
//    self.minusBtn.frame = CGRectMake(0, 5, 20, 20);
//    self.countLb.frame = CGRectMake(self.minusBtn.rb_right, 5, 30, 20);
//    self.addBtn.frame = CGRectMake(self.countLb.rb_right, 5, 20, 20);
//    [self.minusAddView addSubview:self.minusBtn];
//    [self.minusAddView addSubview:self.countLb];
//    [self.minusAddView addSubview:self.addBtn];
//}
//!!!!:getter
- (UILabel *)originPriceLb {
    if (!_originPriceLb) {
        _originPriceLb = [UILabel new];
//        _originPriceLb.backgroundColor = [UIColor yellowColor];
        _originPriceLb.textColor = [UIColor lightGrayColor];
        _originPriceLb.textAlignment = NSTextAlignmentCenter;
//        _originPriceLb.text = @"€0.0";
//        _originPriceLb.hidden = YES;  //12-5隐藏
        //2019-1-10显示
    }
    return _originPriceLb;
}
- (UILabel *)discountPriceLb {
    if (!_discountPriceLb) {
        _discountPriceLb = [[UILabel alloc] init];
        _discountPriceLb.textColor = [UIColor redColor];
        _discountPriceLb.text = @"€0.0";
        _discountPriceLb.textAlignment = NSTextAlignmentCenter;
    }
    return _discountPriceLb;
}


//////////////
- (UIView *)minusAddView {
    if (!_minusAddView) {
        _minusAddView = [UIView new];
//        _minusAddView.backgroundColor = [UIColor brownColor];
    }
    return _minusAddView;
}
- (UIButton *)minusBtn {
    if (!_minusBtn) {
        _minusBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_minusBtn setImage:[UIImage imageNamed:@"jian"] forState:(UIControlStateNormal)];
        [_minusBtn addTarget:self action:@selector(clickMinusBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _minusBtn;
}

- (UILabel *)countLb {
    if (!_countLb) {
        _countLb = [UILabel new];
        _countLb.textAlignment = NSTextAlignmentCenter; //居中
        _countLb.text = @"1"; //默认是1
    }
    return _countLb;
}
- (UIButton *)addBtn {
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_addBtn setImage:[UIImage imageNamed:@"jia"] forState:(UIControlStateNormal)];
        [_addBtn addTarget:self action:@selector(clickAddBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _addBtn;
}

- (UIButton *)addCartButtton {
    if (!_addCartButtton) {
        _addCartButtton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        //国际化
        NSString *addcartStr = NSLocalizedString(Home_AddCart, @"加入购物车");
        NSString *arrowStr = [NSString stringWithFormat:@"%@",addcartStr];
        [_addCartButtton setTitle:arrowStr forState:(UIControlStateNormal)];
        [_addCartButtton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [_addCartButtton.titleLabel setFont:[UIFont systemFontOfSize:15.0]];
        [_addCartButtton setImage:[UIImage imageNamed:@"leftArrow"] forState:(UIControlStateNormal)];
        _addCartButtton.backgroundColor = kMainRedColor;
        [_addCartButtton addTarget:self action:@selector(clickAddCartButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _addCartButtton;
}


//!!!!:重新设置数据
- (void)updateOriginPrice:(NSNumber *)originPrice discountPrice:(NSNumber *)discountPrice {
    self.discountPriceLb.text = [NSString stringWithFormat:@"€%.1f",[discountPrice floatValue]];
    



    //中划线
    NSString *centerLineStr = [NSString stringWithFormat:@"€%.1f",[originPrice floatValue]];
    //文字带中划线
    NSDictionary *attributeDic = @{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:centerLineStr attributes:attributeDic];
    self.originPriceLb.attributedText = attributeStr;
    
    
    //2019-2-16否则就是原价不等于0，并且与现价不相等，显示原价
    if ([originPrice isEqualToNumber:discountPrice] || [originPrice floatValue] < 0.1) {
        self.originPriceLb.hidden = YES;
    }else {
        self.originPriceLb.hidden = NO;
    }
}

//!!!!:事件监听

/**减少*/
- (void)clickMinusBtnAction:(UIButton *)btn {
    if (_cnt <= 1) { //必须跌买一个
        _cnt = 1;
    }else {
        _cnt--;
    }
    
    //更新文字
    self.countLb.text = [NSString stringWithFormat:@"%ld",_cnt];
}


/**添加*/
- (void)clickAddBtnAction:(UIButton *)btn {
    _cnt++;
    //更新文字
    self.countLb.text = [NSString stringWithFormat:@"%ld",_cnt];
}

/**添加到购物车*/
- (void)clickAddCartButtonAction:(UIButton *)btn
{
    //2019-2-16 防止按钮重复点击
    btn.enabled = NO;
    
    FDLog(@"添加到购物车");
    if (self.clickAddCartBlock) {
        self.clickAddCartBlock(_cnt);
    }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        btn.enabled = YES;
    });
}


@end
